package com.lizardmind.haofudoctor.ui.diagnosis.diagnose

import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.LinearLayout
import com.blankj.utilcode.util.SizeUtils
import com.blankj.utilcode.util.TimeUtils
import com.linxiao.framework.activity.BaseActivity
import com.linxiao.framework.util.color
import com.lizardmind.haofudoctor.R
import com.lizardmind.haofudoctor.manager.ImgManager
import com.lizardmind.haofudoctor.manager.postApi
import com.lizardmind.haofudoctor.manager.setImageUrl
import com.lizardmind.haofudoctor.other.DiagnoseDocumentsBean
import com.lizardmind.haofudoctor.other.DoctorInfo
import com.lizardmind.haofudoctor.other.KaiFangFinishEvent
import com.lizardmind.haofudoctor.other.Request
import com.lizardmind.haofudoctor.ui.diagnosis.sickMan.advisory.kaifang.KaiFangActivity
import com.luck.picture.lib.PictureSelector
import com.luck.picture.lib.config.PictureMimeType
import com.luck.picture.lib.entity.LocalMedia
import io.reactivex.functions.Consumer
import kotlinx.android.synthetic.main.activity_look_diagnose.*
import kotlinx.android.synthetic.main.include_title_text.*
import org.greenrobot.eventbus.Subscribe
import org.jetbrains.anko.*
import org.jetbrains.anko.sdk25.coroutines.onClick

/**
 * 查看问诊单数据(医生)
 * Created by Extends on 2017/11/14 16:25
 */
class LookDiagnoseActivity:BaseActivity() {
    private val id by lazy { intent.getStringExtra("id") }
    private lateinit var bean:DiagnoseDocumentsBean
    override fun onCreatRootView(): View? {
        setContentView(R.layout.activity_look_diagnose)
        return null
    }

    override fun onInitView(savedInstanceState: Bundle?) {
        setEvent(true)
        //初始化title
        imgLeft.setImageResource(R.mipmap.back)
        imgLeft.onClick { finish() }
        titleText.text = "问诊单"

        observe {
            postApi<Request<DiagnoseDocumentsBean>>(hashMapOf(
                    "action" to "diagnose_info",
                    "token" to DoctorInfo.token,
                    "id" to id
            ), Consumer {
                it.dispose {
                    bean = it
                    init()
                }
            })
        }

    }

    fun init(){
        //基本信息
        bean.fill_user.apply {
            ald_name.text=name+" "+(if(sex==1) "男" else "女")+" "+age+"岁"
            ald_time.text = TimeUtils.millis2String(bean.complete_time*1000L)
            ald_birthday.text = birthday
            ald_height.text = getString(R.string.height,height)
            ald_weight.text = getString(R.string.weight,weight)
            ald_bmi.text = bmi

            ald_family.text = bingshi(family_history)
            ald_disease.text = bingshi(disease_history)
            ald_surgery.text = bingshi(surgery_history)
            ald_medication.text = bingshi(medication_history)
            ald_allergy.text = bingshi(allergy_history)
        }

        //患者主诉
        ald_describe.text = bean.describe
        val lp = LinearLayout.LayoutParams(SizeUtils.dp2px(57f),SizeUtils.dp2px(57f))
        lp.marginStart = SizeUtils.dp2px(5f)
        ald_tongue.apply {
            val list = bean.tongue_picture.map { LocalMedia(ImgManager.IMGURL+it,0,PictureMimeType.ofImage(),PictureMimeType.JPEG)}
            bean.tongue_picture.forEachIndexed { i,it->
                imageView {
                    layoutParams = lp
                    setImageUrl(it)
                    onClick {
                        PictureSelector.create(this@LookDiagnoseActivity).externalPicturePreview(i,list)
                    }
                }
            }
        }
        ald_relate.apply {
            val list = bean.related_picture.map { LocalMedia(ImgManager.IMGURL+it,0,PictureMimeType.ofImage(),PictureMimeType.JPEG)}
            bean.related_picture.forEachIndexed { i,it->
                imageView {
                    layoutParams = lp
                    setImageUrl(it)
                    onClick {
                        PictureSelector.create(this@LookDiagnoseActivity).externalPicturePreview(i,list)
                    }
                }
            }
        }

        //问题概要
        ald_problem.apply {
            bean.answer_details.forEach {
                include<View>(R.layout.include_split_line)
                textView {
                    text = it.type_title
                    textColor = context.color(R.color.gray3)
                    setPadding(SizeUtils.dp2px(16f),SizeUtils.dp2px(16f),SizeUtils.dp2px(16f),SizeUtils.dp2px(8f))
                }
                textView {
                    text=it.type_answer
                    textSize = 13f
                    textColor = context.color(R.color.gray2)
                    setPadding(SizeUtils.dp2px(16f),SizeUtils.dp2px(0f),SizeUtils.dp2px(16f),SizeUtils.dp2px(10f))
                }
            }
        }

        //备注
        ald_note.setText(bean.note)
        ald_saveNote.onClick {
            val tempNote = ald_note.text.toString()
            if(tempNote==bean.note){
                toast("备注没有变化，不需要保存")
            }else{
                observe {
                    postApi<Request<String>>(hashMapOf(
                            "action" to "save_diagnose_note",
                            "token" to DoctorInfo.token,
                            "id" to bean.id,
                            "note" to tempNote
                    ), Consumer {
                        it.dispose {
                            toast(it)
                        }
                    })
                }
            }
        }
        if(bean.is_prescribing==0){
            ald_prescribing.isEnabled = true
            ald_prescribing.backgroundColor = color(R.color.notice_red)
            ald_prescribing.text = "为TA开方"
        }else{
            ald_prescribing.isEnabled = false
            ald_prescribing.backgroundColor = color(R.color.unChecked)
            ald_prescribing.text = "已开方"
        }

        //开方
        ald_prescribing.onClick {
            //跳转到开方界面
            startActivity<KaiFangActivity>("userBean" to bean.fill_user,"father_id" to bean.id,"father_type" to "1","uid" to bean.user_info.uid)
        }
    }

    fun bingshi(string: String):String = if(string.isEmpty()) "未填写" else string

    @Subscribe
    fun getFinishInform(event: KaiFangFinishEvent){
        ald_prescribing.isEnabled = false
        ald_prescribing.backgroundColor = color(R.color.unChecked)
        ald_prescribing.text = "已开方"
    }
}